package com.inossem.warehousestorage_inossem.activity.change_storage.chuangyou;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.QRCodeUtils;
import com.inossem.utils.constant.QRCodeConstant;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.utils.log.LogUtils;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.adapter.changeStorage.chuangyou.CYChangeStorageSelectPurchaseOrderAdapter;
import com.inossem.warehousestorage_inossem.bean.changeStorage.chuangyou.CYChangeStorageBean;
import com.inossem.warehousestorage_inossem.bean.changeStorage.chuangyou.CYChangeStoragePurchaseOrderBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.changeStorage.chuangyou.CYChangeStorageSearchPurchaseOrderRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.CYChangeStorageUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * TODO
 *
 * @author 王斯宇
 * @time on 2018/3/21 17:34
 * @email siyu.wang@inossem.com
 */
public class CYChangeStorageSearchPurchaseOrderActivity extends BaseActivity {
    private TextView title;

    private PullableListView listView;

    private CYChangeStorageSelectPurchaseOrderAdapter adapter;

    private List<CYChangeStoragePurchaseOrderBean.PurchaseOrderBean> purchaseOrderBeanList;

    private String condition;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.cy_actiovity_change_storage_search_purchase_order);
        initView(this);
        initData(this);
    }

    @Override
    protected void onResumeImpl() {

    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        listView = (PullableListView) activity.findViewById(R.id.list);
    }

    private void initData(Activity activity) {
        title.setText("采购订单");
        ActivityUtils.initBackAndFunction(activity, activity.getResources().getString(R.string.activity_inventory_item_title), new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });
        listView.setPullUpFlag(false);
        listView.setPullDownFlag(false);
        purchaseOrderBeanList = CYChangeStorageUtils.getPurchaseOrderList(activity);
        setmScanListener((String scanResult, int length) -> {
            String mCode = QRCodeUtils.DecryptQRCode(scanResult);

        },  QRCodeConstant.TYPE_MATERIEL_CODE);
        addPurchaseOrderListData(activity , listView , true);
    }

    private void getData (Context context ) {
        CYChangeStorageSearchPurchaseOrderRequest.request(context, condition, new GsonBeanListener<CYChangeStoragePurchaseOrderBean>() {
            @Override
            public void onSuccess(CYChangeStoragePurchaseOrderBean bean) {
                if (purchaseOrderBeanList.size() == 0) {
                    purchaseOrderBeanList = bean.getBody();
                }else {
                    purchaseOrderBeanList = CYChangeStorageUtils.getCYChangeStoragePurchaseOrderBean(context , bean.getBody());
                }
                CYChangeStorageUtils.savePurchaseOrder(context , purchaseOrderBeanList);
                addPurchaseOrderListData(context , listView , true);
            }
        });
    }
    private void addPurchaseOrderListData(Context context, ListView listView ,  boolean flag) {
        if (flag) {
            adapter = new CYChangeStorageSelectPurchaseOrderAdapter(context , condition , purchaseOrderBeanList , new CYChangeStorageSearchPurchaseOrderListener() {
                @Override
                public void onSearchClickListener(String purchaseOrderNumber) {
                    condition = purchaseOrderNumber;
                    getData(context);
                }

                @Override
                public void onViewCliekListener(String purchaseOrderrrNumber) {
                    DialogUtils.getConfirmDialog(context, "是否删除？", new DialogUtils.ConfirmDialogListener() {
                        @Override
                        public void onConfirm(Dialog dialog) {
                            dialog.dismiss();
                            CYChangeStorageUtils.deletePurchaseOrder(context , purchaseOrderrrNumber);
                            purchaseOrderBeanList = CYChangeStorageUtils.getPurchaseOrderList(context);
                            addPurchaseOrderListData(context , listView , true);
                        }
                    }).show();
                }
            });
            listView.setAdapter(adapter);
        }
    }

    public interface CYChangeStorageSearchPurchaseOrderListener {
        void onSearchClickListener(String purchaseOrderrrNumber);
        void onViewCliekListener(String purchaseOrderrrNumber);
    }
}
